package com.groupbuying.provider.service.impl;

import com.groupbuying.provider.constants.CommonConstants;
import com.groupbuying.provider.context.FilterContextHandler;
import com.groupbuying.provider.dao.StoreCompanyInfoDao;
import com.groupbuying.provider.dao.StoreMainInfoDao;
import com.groupbuying.provider.domain.StoreCompanyInfoDO;
import com.groupbuying.provider.domain.StoreMainInfoDO;
import com.groupbuying.provider.dto.StoreMainDTO;
import com.groupbuying.provider.dto.convert.StoreMainInfoConvert;
import com.groupbuying.provider.pojo.StoreMainInfoEnum;
import com.groupbuying.provider.service.StoreCompanyInfoService;
import com.groupbuying.provider.service.StoreMainInfoService;
import com.groupbuying.provider.utils.PageUtils;
import com.groupbuying.provider.utils.Query;
import com.groupbuying.provider.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class StoreCompanyInfoServiceImpl implements StoreCompanyInfoService {
	@Autowired
	private StoreCompanyInfoDao storeCompanyInfoDao;
	@Autowired
	private StoreMainInfoDao storeMainInfoDao;
	@Autowired
	private StoreMainInfoService storeMainInfoService;



	@Override
	public List<StoreCompanyInfoDO> list(Map<String, Object> map){
		return storeCompanyInfoDao.list(map);
	}
	


	@Override
	public StoreCompanyInfoDO getCompanyInfoByStoreUuid(String storeUuid) {

		Map<String, Object> params = new HashMap<>();
		params.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
		params.put("storeuuid", storeUuid);
		List<StoreCompanyInfoDO> list = storeCompanyInfoDao.list(params);
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	@Override
	public R getShopListByProviderId(Map<String, Object> params) {
		try {
			params.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
			// 服务商id
			params.put("chainstoresuuid", FilterContextHandler.getName());
			params.put("sort", "createOpeTime");
			params.put("order", "desc");
			Query query = new Query(params);
			// TODO: 缺少DTO
            /* List<StoreMainInfoDO> list = storeMainInfoService.getStoreListByProviderIdAndAccountId(query);
        int total = storeMainInfoService.getStoreListByProviderIdAndAccountIdCount(query);*/
			List<StoreMainInfoDO> list = storeMainInfoDao.getShopList(query);
			List<StoreMainDTO> dtos = new ArrayList<StoreMainDTO>(list.size());
			for (StoreMainInfoDO storeMainInfoDO : list) {
				switch (storeMainInfoDO.getAuditstatus()) {
					case StoreMainInfoEnum.AUDIT_UNDONE: {
						storeMainInfoDO.setAuditstatus("审核中");
					}
					break;
					case StoreMainInfoEnum.BUSINESS_AUDIT_PASS: {
						storeMainInfoDO.setAuditstatus("业务人员审核通过");
					}
					break;
					case StoreMainInfoEnum.AUDIT_NO_PASS: {
						storeMainInfoDO.setAuditstatus("审核驳回");
					}
					break;
					case StoreMainInfoEnum.REPORT_UNDONE: {
						storeMainInfoDO.setAuditstatus("报备失败");
					}
					break;
					case StoreMainInfoEnum.FINANCE_AUDIT_PASS: {
						storeMainInfoDO.setAuditstatus("财务人员审核通过");
					}
					break;
				}
				StoreCompanyInfoDO storeCompanyInfoDO = getCompanyInfoByStoreUuid(storeMainInfoDO.getUuid());
				StoreMainDTO storeMainDTO = StoreMainInfoConvert.MAPPER.doToDTO(storeMainInfoDO);
				storeMainDTO.setCompanytype(storeCompanyInfoDO.getCompanytype());
				dtos.add(storeMainDTO);
			}
			int total = storeMainInfoDao.getShopCount(query);
			PageUtils pageUtil = new PageUtils(dtos, total);
			return R.ok().put("page", pageUtil);
		} catch (Exception e) {
			return R.error();
		}
	}
	@Override
	public R getStoreByProviderId(Map<String, Object> params) {
		try {
			params.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
			// 服务商id
			params.put("chainstoresuuid", FilterContextHandler.getName());
			params.put("sort", "createOpeTime");
			params.put("order", "desc");
			Query query = new Query(params);
			// TODO: 缺少DTO
            /* List<StoreMainInfoDO> list = storeMainInfoService.getStoreListByProviderIdAndAccountId(query);
        int total = storeMainInfoService.getStoreListByProviderIdAndAccountIdCount(query);*/
			List<StoreMainInfoDO> list = storeMainInfoService.list(query);
			List<StoreMainDTO> dtos = new ArrayList<StoreMainDTO>(list.size());
			for (StoreMainInfoDO storeMainInfoDO : list) {
				switch (storeMainInfoDO.getAuditstatus()) {
					case StoreMainInfoEnum.AUDIT_UNDONE: {
						storeMainInfoDO.setAuditstatus("审核中");
					}
					break;
					case StoreMainInfoEnum.BUSINESS_AUDIT_PASS: {
						storeMainInfoDO.setAuditstatus("业务人员审核通过");
					}
					break;
					case StoreMainInfoEnum.AUDIT_NO_PASS: {
						storeMainInfoDO.setAuditstatus("审核驳回");
					}
					break;
					case StoreMainInfoEnum.REPORT_UNDONE: {
						storeMainInfoDO.setAuditstatus("报备失败");
					}
					break;
					case StoreMainInfoEnum.FINANCE_AUDIT_PASS: {
						storeMainInfoDO.setAuditstatus("财务人员审核通过");
					}
					break;
				}
				StoreCompanyInfoDO storeCompanyInfoDO = getCompanyInfoByStoreUuid(storeMainInfoDO.getUuid());
				StoreMainDTO storeMainDTO = StoreMainInfoConvert.MAPPER.doToDTO(storeMainInfoDO);
				storeMainDTO.setCompanytype(storeCompanyInfoDO.getCompanytype());
				dtos.add(storeMainDTO);
			}
			int total = storeMainInfoService.count(query);
			PageUtils pageUtil = new PageUtils(dtos, total);
			return R.ok().put("page", pageUtil);
		} catch (Exception e) {
			return R.error();
		}
	}

}
